﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
//using System.Text.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Data;
using System.Linq;
using WebApplication1.Interfaces;
using WebApplication1.Models;
 
 



// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace WebApplication1.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class IndexController : ControllerBase
    {

        private readonly ILegacyService _legacyService;
         
        public IndexController(ILegacyService legacyService)
        {
            _legacyService = legacyService;
        }

         

        // POST api/<IndexController>
        [HttpPost]
        //public void Post([FromBody] LoginModel login)
        public async Task<ActionResult> Post([FromBody] LoginModel login)
        {

            string appid        = login.APPID;
            string staff_id     = login.USERID;

            string str_temp = "  select TA.EXTEND_2 from    "
                            + "  tpabma22 TA, tqmcca01 TB    "
                            + "   where TA.func_name =  '" + appid + "' "
                            + "   AND TB.WEIXIN_NO = '" + staff_id + "' "
                            + "   AND TB.FORM_NAME_ROLE LIKE '%' || TA.ROLE || '%' "
                            ;





            DataSet DS_tpabma22 = await _legacyService.CallLegacyMethodAsync(str_temp);

            


            string str_form_name = "";
            for (int i = 0; i < DS_tpabma22.Tables[0].Rows.Count; i++)
            {
                str_form_name = str_form_name + DS_tpabma22.Tables[0].Rows[i][0].ToString() + ",";
            }



            string str1 = " select distinct class_code,class_desc,type_no  from  tpabma21 "
                        + " where   func_name = '" + appid + "'  "
                        + " and  '" + str_form_name + "' LIKE '%' ||FORM_NO || '%' "
                        + " order by  type_no"
                        ;

            string str2 = "   select * from  tpabma21     "
                     + "   where   func_name = '" + appid + "'  "
                     + "   and  '" + str_form_name + "' LIKE '%' || FORM_NO || '%' "
                     + "   order by  type_no,seq_now   "
                     ;


          

            
            DataSet DS_T1 = await _legacyService.CallLegacyMethodAsync(str1);
            DataSet DS_T2 = await _legacyService.CallLegacyMethodAsync(str2);





            var result = new List<dynamic>();
            DataTable dtTemp = null;
            string class_code = string.Empty;

            for (int i = 0; i < DS_T1.Tables[0].Rows.Count; i++)
            {
                var classObj = new
                {
                    class_desc = DS_T1.Tables[0].Rows[i]["CLASS_DESC"].ToString(),
                    items = new List<dynamic>()
                };

                class_code = DS_T1.Tables[0].Rows[i]["CLASS_CODE"].ToString();

                if (DS_T2.Tables[0].Select("CLASS_CODE  = '" + class_code + "'").Count<DataRow>() >= 1)
                {
                    dtTemp = DS_T2.Tables[0].Select("CLASS_CODE  = '" + class_code + "'").CopyToDataTable<DataRow>();

                    //子项明细；
                    for (int j = 0; j < dtTemp.Rows.Count; j++)
                    {
                        classObj.items.Add(new
                        {
                            link_address = dtTemp.Rows[j]["LINK_ADDRESS"].ToString(),
                            menu_icon = dtTemp.Rows[j]["MENU_ICON"].ToString(),
                            form_name = dtTemp.Rows[j]["FORM_NAME"].ToString(),
                        });
                    }
                }

                result.Add(classObj);

            }



            //string sa= "asdf";
            //string str= Ok(result);
            //  DataSet DS_tpabma22 = JsonSerializer.Deserialize<DataSet>(sa);

            //  JsonSerializer.Deserialize<User>(redisHelper.GetValue("user:1"));



            /*
            EI.EIInfo outBlock_temp = EF.Utility.ExecQuery(str_temp);

          


         



            EI.EIInfo outBlock = EF.Utility.ExecQuery(str1);
            EI.EIInfo outBlock2 = EF.Utility.ExecQuery(str2);

         

            */


            return Ok(result);

        }



        /*多条记录一办法获取方法
         *  public IActionResult CreateMultiple([FromBody] List<Person> peoples)
         *  
         *  
         *  但记录一般获取前台参数方法
         *  public IActionResult CreateMultiple([FromBody] Person people)
        */

        // POST api/<IndexController>
        [HttpPost]
        //public void Post([FromBody] LoginModel login)
        public async Task<ActionResult> Post2()
        {

            var rawJson = await Request.GetRawBodyAsync();

            //放在html中处理；
           // rawJson = "{ \"table0\": [ "+ rawJson + " ]}";

            

            string DS_tpabma22 = await _legacyService.CallService_json("qmcc801_web_inq", rawJson);

            return Ok(DS_tpabma22);


            //string sa= "asdf";
            //string str= Ok(result);
            //DataSet DS_tpabma22 = JsonSerializer.Deserialize<DataSet>(sa);

            //JsonSerializer.Deserialize<User>(redisHelper.GetValue("user:1"));



            /*
            EI.EIInfo outBlock_temp = EF.Utility.ExecQuery(str_temp);

          

            EI.EIInfo outBlock = EF.Utility.ExecQuery(str1);
            EI.EIInfo outBlock2 = EF.Utility.ExecQuery(str2);

         

            */


            //  return Ok(result);

        }



       





        [HttpPost]
        public async Task<ActionResult> Login3([FromBody] LoginModel username)
        {
            var result = await _legacyService.CallLegacyMethodAsync(username.username);
            return Ok(result);
        }





        // PUT api/<IndexController>/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] string value)
        {
        }

        // DELETE api/<IndexController>/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }
}
